<template>
  <div>
    <Header>数据看板</Header>
    <div class="datarow">
      <div class="data-item c1">
        <div class="icon">
          <el-icon :size="25" color="#FFA42F">
            <i-ep-Postcard />
          </el-icon>
        </div>
        <div class="content">
          <p>注册用户总数：{{ user_count }}</p>
        </div>
      </div>
      <div class="data-item c2">
        <div class="icon">
          <el-icon :size="25" color="#FB575E">
            <i-ep-document />
          </el-icon>
        </div>
        <div class="content">
          <p>用户简历总数：{{ recruit_count }}</p>
        </div>
      </div>
    </div>
    <div class="chart" id="chart"></div>
  </div>
</template>

<script setup>
import { inject, onMounted,ref } from 'vue'
import { bar_option } from '@/config/echartsConfig.js'
const $echarts = inject('$echarts')
const $root = inject('$root')
const $axios = inject('$axios')
let user_count = ref(0)
let recruit_count = ref(0)
let job_name = ref([])
let job_recruit = ref([])
/* 初始化和配置echarts */
const myEcharts = (e, opt) => {
  let myChart = $echarts.init(document.querySelector(e))
  myChart.setOption(opt)
  window.addEventListener('resize', function () {
    myChart.resize()
  })
}
/* 可视化数据获取 */
const traverse = () => {
 return $axios.get($root + `/visual/`)
    .then(r => {
      if (r.data.code == 200) {
        user_count.value = r.data.data.user_count
        recruit_count.value = r.data.data.recruit_count
        r.data.data.hot_recruit.forEach( item=>{
          job_name.value.push(item.job_name)
          job_recruit.value.push(item.apply_num)
        } )
        bar_option.yAxis.data = job_name.value
        bar_option.series[0].data = job_recruit.value
      }
    })
}

onMounted(() => {
  traverse().then( ()=>{
    myEcharts('#chart', bar_option)
  } )
})

</script>

<style scoped>
.datarow {
  width: 90%;
  margin: 20px auto;
  display: flex;
}

.data-item {
  width: 250px;
  height: 80px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  color: white;
  padding: 15px;
  box-sizing: border-box;
  margin-right: 20px;
  font-size: 18px;
  margin-top: 10px;
}

.data-item:hover {
  box-shadow: 0 0 3px #555555;
  cursor: pointer;
}

.c1 {
  background: #FFA42F;
}

.c2 {
  background: #FB575E;
}

.icon {
  width: 40px;
  height: 40px;
  border-radius: 30px;
  background: white;
  text-align: center;
  padding: 7px;
  box-sizing: border-box;
}

.content {
  margin-left: 15px;
}

.chart {
  width: 90%;
  border-radius: 6px;
  margin: 30px auto;
  box-shadow: 0 0 5px #ccc;
  height: 300px;
  padding: 20px 0 0 0;
}
</style>